package com.zch.house.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zch.house.entity.House;
import com.zch.house.service.HouseService;
import com.zch.house.service.impl.HouseServiceImpl;
import com.zch.house.util.DateFormat;
import com.zch.house.util.SplitStr;

/**
 * Servlet implementation class HouseServlet
 */
@WebServlet("/HouseServlet2")
public class HouseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    HouseService hs = new HouseServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HouseServlet() {
        super();
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if("add".equals(method)){
			add(request,response);
		}else if("delete".equals(method)){
			delete(request,response);
		}else if("update".equals(method)){
			update(request,response);
		}else if("select".equals(method)){
			select(request,response);
		}else if("getHouseById".equals(method)){
			getHouseById(request,response);
		}
	}

	private void getHouseById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		House house = hs.getHouseById(id);
		request.setAttribute("house", house);
		request.getRequestDispatcher("view/house/edit.jsp").forward(request, response);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String area = request.getParameter("area");
		String estate = request.getParameter("estate");
		String unitNumber = request.getParameter("unitNumber");
		String floorStr = request.getParameter("floor"); 
		int floor = Integer.parseInt(floorStr);
		String roomNo = request.getParameter("roomNo");
		String acreage = request.getParameter("acreage");
		String direction = request.getParameter("direction");
		String fitment = request.getParameter("fitment");
		String isDoubleAirStr = request.getParameter("isDoubleAir");
		int isDoubleAir = Integer.parseInt(isDoubleAirStr);
		String limitStr = request.getParameter("limit");
		int limit = Integer.parseInt(limitStr);
		String facility = request.getParameter("facility");
		String priceStr = request.getParameter("price");
		double price = Double.parseDouble(priceStr);
		String statusStr = request.getParameter("status");
		int status = Integer.parseInt(statusStr);
		String imgStr = request.getParameter("img");
		System.out.println("imgStr="+imgStr);
		String img = SplitStr.splitStr(imgStr, "\\\\");
		System.out.println("**************"+img);
		String address = request.getParameter("address");
		String addTimeStr = request.getParameter("addTime");
		Date addTime = DateFormat.to_Date2(addTimeStr);
		String updateTimeStr = request.getParameter("updateTime");
		Date updateTime = DateFormat.to_Date2(updateTimeStr);
		House house = new House(area, estate, unitNumber, floor, roomNo, 
				acreage, direction, fitment, isDoubleAir, limit, facility,
				price, status, img, address, addTime, updateTime);
		boolean flag = hs.addHouse(house);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将添加成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加成功！');window.location.href='HouseServlet2?method=select'</script>");
		} else {
			// 添加失败,将添加失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加失败！');window.location.href='HouseServlet2?method=select'</script>");
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		boolean flag = hs.deleteHouse(id);
		// 实例化一个out对象
		PrintWriter out = response.getWriter();
		if (flag) {
			// 删除成功,将删除成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除成功！');window.location.href='HouseServlet2?method=select'</script>");
		} else {
			// 删除失败,将删除失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除失败！');window.location.href='HouseServlet2?method=select'</script>");
		}
	
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		String area = request.getParameter("area");
		String estate = request.getParameter("estate");
		String unitNumber = request.getParameter("unitNumber");
		String floorStr = request.getParameter("floor"); 
		int floor = Integer.parseInt(floorStr);
		String roomNo = request.getParameter("roomNo");
		String acreage = request.getParameter("acreage");
		String direction = request.getParameter("direction");
		String fitment = request.getParameter("fitment");
		String isDoubleAirStr = request.getParameter("isDoubleAir");
		int isDoubleAir = Integer.parseInt(isDoubleAirStr);
		String limitStr = request.getParameter("limit");
		int limit = Integer.parseInt(limitStr);
		String facility = request.getParameter("facility");
		String priceStr = request.getParameter("price");
		double price = Double.parseDouble(priceStr);
		String statusStr = request.getParameter("status");
		int status = Integer.parseInt(statusStr);
		String imgStr = request.getParameter("img");
		System.out.println("imgStr="+imgStr);
		String img = SplitStr.splitStr(imgStr, "\\\\");
		String address = request.getParameter("address");
		String addTimeStr = request.getParameter("addTime");
		Date addTime = DateFormat.to_Date2(addTimeStr);
		String updateTimeStr = request.getParameter("updateTime");
		Date updateTime = DateFormat.to_Date2(updateTimeStr);
		House house = new House(id, area, estate, unitNumber, 
				floor, roomNo, acreage, direction, fitment,
				isDoubleAir, limit, facility, price, status, 
				img, address, addTime, updateTime);
		//调用service层方法
		boolean flag = hs.updateHouse(house);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 更新成功,将更新成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('更新成功！');window.location.href='HouseServlet2?method=select'</script>");
		} else {
			// 更新失败,将更新失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('更新失败！');window.location.href='HouseServlet2?method=select'</script>");
		}
	}

	private void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//详情
		String flag = request.getParameter("flag");
		String idStr = request.getParameter("id");
		
		if(flag==null){
			String h_area = request.getParameter("area");
			String h_estate = request.getParameter("estate");
			String h_acreage = request.getParameter("acreage");
			
			String lowPriceStr = request.getParameter("lowPrice");
			if(lowPriceStr==null||lowPriceStr==""){
				lowPriceStr = "-1";
			}
			double lowPrice = Double.parseDouble(lowPriceStr);
			
			String highPriceStr = request.getParameter("highPrice");
			if(highPriceStr==null||highPriceStr==""){
				highPriceStr = "-1";
			}
			double highPrice = Double.parseDouble(highPriceStr);
			
			String h_statusStr = request.getParameter("status");
			if(h_statusStr==null||h_statusStr==""){
				h_statusStr = "-1";
			}
			int h_status = Integer.valueOf(h_statusStr);
			String pageIndexStr = request.getParameter("pageIndex");
			if(pageIndexStr == null||pageIndexStr == ""){
				pageIndexStr = "1";
			}
			int pageIndex = Integer.parseInt(pageIndexStr);
			
			//获得总的数据条数
			int totalCounts = hs.getTotalCount2(h_area, h_estate, h_acreage, lowPrice, highPrice, h_status);
			//获得房屋列表
			List<House> list = hs.getAllHouseByPage(h_area, h_estate, h_acreage, 
					lowPrice, highPrice, h_status, pageIndex);
			int totalPages = hs.getTotalCount(h_area, h_estate, h_acreage, lowPrice, highPrice, h_status);
			//将参数存入作用域
			request.setAttribute("area", h_area);
			request.setAttribute("estate", h_estate);
			request.setAttribute("acreage", h_acreage);
			request.setAttribute("totalCounts", totalCounts);
			if(lowPrice==-1){
				request.setAttribute("lowPrice", null);
			}else{
				request.setAttribute("lowPrice", lowPrice);
			}
			if(highPrice==-1){
				request.setAttribute("highPrice", null);
			}else{
				request.setAttribute("highPrice", highPrice);
			}
			if(h_status==-1){
				request.setAttribute("status", null);
			}else{
				request.setAttribute("status", h_status);
			}
			request.setAttribute("pageIndex", pageIndex);
			request.setAttribute("list", list);
			request.setAttribute("totalPages", totalPages);
			request.getRequestDispatcher("view/house/list.jsp").forward(request, response);
		}else if("detail".equals(flag)){
			if(idStr!=null&&idStr!=""){
				int id = Integer.parseInt(idStr);
				House house = hs.getHouseById(id);
				System.out.println(house.toString());
				request.setAttribute("house", house);
				request.getRequestDispatcher("view/house/detail.jsp").forward(request, response);
			}
		}	
	}

}
